package com.gwtapps.multisearch.client.view;



import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DisclosurePanelImages;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtapps.client.ui.Link;
import com.gwtapps.multisearch.client.model.SearchEngine;
import com.gwtapps.multisearch.client.model.SearchEngineResult;

public class SearchResultsView extends Composite {  
	
	public static interface SearchResultImages extends DisclosurePanelImages {
		/**
		 * @gwt.resource minimize1.gif
		 */
		AbstractImagePrototype disclosurePanelOpen();
		  
		/**
		 * @gwt.resource minimize2.gif
		 */
		AbstractImagePrototype disclosurePanelClosed();
	
	}
	public static SearchResultImages searchResultImages = (SearchResultImages) GWT.create( SearchResultImages.class );

	private DisclosurePanel mainPanel = null;
	private VerticalPanel results = new VerticalPanel();
	private SearchEngine engine;
	
	public SearchResultsView( SearchEngine engine ){
		this.engine = engine;
		mainPanel = new DisclosurePanel( searchResultImages,engine.getName(),true );
		initWidget( mainPanel );
		mainPanel.setContent(results);
		mainPanel.setStyleName("searchResultsView");
	}
	
	public void clearResults(){
		results.clear();
		mainPanel.setOpen(true);
	}
	
	public void addSearchResult( final SearchEngineResult result ){
		VerticalPanel searchResultPanel = new VerticalPanel();
		Link title = new Link( result.getTitle(), result.getUrl() );
		HTML description = new HTML( result.getDescription() );
		
		HTML author = new HTML ("<font color=red>Author:"+result.getAuthor()+"</font>");
		HTML published = new HTML ("<font color=red>Published Date:"+result.getPublished()+"</font>");
		HTML category = new HTML ("<font color=red>Category:"+result.getCategory()+"</font>");
		HTML keywords = new HTML ("<font color=red>Keywords:"+result.getKeywords()+"</font>");
		HTML viewCount = new HTML ("<font color=red>View Count:"+result.getViewCount()+"</font>");
		HTML favoriteCount = new HTML ("<font color=red>Favorite Count:"+result.getFavoriteCount()+"</font>");
		HTML numRaters = new HTML ("<font color=red>Number of Raters:"+result.getNumRaters()+"</font>");
		HTML average = new HTML ("<font color=red>Average Rate:"+result.getAverage()+"</font>");
		HTML thumbnail1 = new HTML ("<img src=\""+result.getThumbnailURL1()+"\"></img>");
		HTML thumbnail2 = new HTML ("<img src=\""+result.getThumbnailURL2()+"\"></img>");
		HTML thumbnail3 = new HTML ("<img src=\""+result.getThumbnailURL3()+"\"></img>");
		HTML thumbnail4 = new HTML ("<img src=\""+result.getThumbnailURL4()+"\" height=\"97\" width=\"130\"></img>");
		Button playButton = new Button ("Play it");
		Button annotateButton = new Button ("Annotate");
		
		playButton.addClickListener( new ClickListener() {
		      public void onClick(Widget sender) {
		    	  Popup playDlg = new Popup(result.getUrl());
		          playDlg.show();
		      }
		    });
		
		
		annotateButton.addClickListener( new ClickListener() {
		      public void onClick(Widget sender) {
		    	  annotatePopup annotateDlg = new annotatePopup(result.getUrl());
		    	  annotateDlg.show();
		      }
		    });
		
		
		searchResultPanel.add( title );
		searchResultPanel.add( description );
		searchResultPanel.add( author );
		searchResultPanel.add( published );
		searchResultPanel.add( category );
		searchResultPanel.add( keywords );
		searchResultPanel.add( viewCount );
		searchResultPanel.add( favoriteCount );
		searchResultPanel.add( numRaters );
		searchResultPanel.add( average );
		searchResultPanel.add( thumbnail1 );
		searchResultPanel.add( thumbnail2 );
		searchResultPanel.add( thumbnail3 );
		searchResultPanel.add( thumbnail4 );
		
		searchResultPanel.add( playButton );
		searchResultPanel.add( annotateButton );
		
		searchResultPanel.setStyleName("searchResultPanel");
		title.setStyleName("searchResultLink");
		description.setStyleName("searchResultDescription");
		results.add( searchResultPanel );
	}
	
	public SearchEngine getEngine() {
		return engine;
	}
}
